package minecraft.xiyu.exchantedbook.procedures;

import minecraft.xiyu.exchantedbook.init.ExchantedBookModEnchantments;
import net.minecraft.client.renderer.item.ItemProperties;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

import java.util.function.Function;

import static minecraft.xiyu.exchantedbook.ExchantedBookMod.MODID;

@Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class AttackspeedEnchantmentEventHandler {

	@SubscribeEvent
	public static void onPlayerTick(TickEvent.PlayerTickEvent event) {
		if (!event.player.level.isClientSide && event.phase == TickEvent.Phase.END) {
			LivingEntity player = event.player;
			ItemStack mainHandItem = player.getMainHandItem();
			int attackspeedLevel = getAttackspeedEnchantmentLevel(mainHandItem);
			int attackspeedSlowLevel = getAttackspeedSlowEnchantmentLevel(mainHandItem);

			AttributeInstance attackSpeedAttribute = player.getAttribute(Attributes.ATTACK_SPEED);
			float baseSpeed = (float) attackSpeedAttribute.getValue();

			if (attackspeedSlowLevel > 0) {
				// 降低攻击速度
				decreaseAttackSpeed(attackSpeedAttribute, attackspeedSlowLevel);
			} else if (attackspeedLevel > 0) {
				// 增加攻击速度
				System.out.print("111");
				increaseAttackSpeed(attackSpeedAttribute, attackspeedLevel);
			} else {
				// 重置攻击速度
				attackSpeedAttribute.setBaseValue(4.0f);
			}
		}
	}


	private static void increaseAttackSpeed(AttributeInstance attackSpeedAttribute, int attackspeedLevel) {
		double baseAttackSpeed = 4.0; // 默认攻击速度
		double increasePerLevel = 0.06; // 每级增加0.06的攻击速度
		double totalIncrease = attackspeedLevel * increasePerLevel;
		double newAttackSpeed = baseAttackSpeed + totalIncrease;

		double maxAttackSpeed = 35; // 最大攻击速度
		newAttackSpeed = Math.min(newAttackSpeed, maxAttackSpeed); // 确保不超过最大攻击速度

		attackSpeedAttribute.setBaseValue((float) newAttackSpeed);
	}

	private static void decreaseAttackSpeed(AttributeInstance attackSpeedAttribute, int attackspeedSlowLevel) {
		double baseAttackSpeed = 4.0; // 默认攻击速度
		double reductionPerLevel = 0.06; // 每级降低0.06的攻击速度
		double totalReduction = attackspeedSlowLevel * reductionPerLevel;
		double newAttackSpeed = baseAttackSpeed - totalReduction;

		double minAttackSpeed = 0.01;
		newAttackSpeed = Math.max(newAttackSpeed, minAttackSpeed);

		attackSpeedAttribute.setBaseValue((float) newAttackSpeed);
	}

	private static int getAttackspeedEnchantmentLevel(ItemStack itemStack) {
		return EnchantmentHelper.getItemEnchantmentLevel(ExchantedBookModEnchantments.ATTACKSPEED.get(), itemStack);
	}

	private static int getAttackspeedSlowEnchantmentLevel(ItemStack itemStack) {
		return EnchantmentHelper.getItemEnchantmentLevel(ExchantedBookModEnchantments.ATTACKSPEEDLOW.get(), itemStack);
	}
}
